package com.useeinfo.asg.open.system;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.security.biz.SystemAuthorityBiz;
import com.useeinfo.asg.modules.security.biz.SystemRoleBiz;
import com.useeinfo.asg.modules.security.entity.SystemAuthority;
import com.useeinfo.asg.modules.security.entity.SystemRole;
import com.useeinfo.asg.modules.security.entity.SystemUser;
import com.useeinfo.framework.sugar.data.QueryParam;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by 宝玉 on 2017/8/23.
 */
@Controller
@RequestMapping("/admin/system/role")
public class SystemRoleAdminManageAction {

    @Autowired
    private SystemRoleBiz systemRoleBiz;
    @Autowired
    private SystemAuthorityBiz systemAuthorityBiz;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, ModelMap mv) {

        return "/system/systemRole/list";
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJosn(@RequestParam(value = "page", required = false) String pageNowParam,
                               @RequestParam(value = "rows", required = false) String pageSizeParam,
                               @RequestParam(value = "sidx", required = false) String sidxParam,
                               @RequestParam(value = "sord", required = false) String sordParam) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);

        return systemRoleBiz.findJSONList(queryParam);
    }

    @RequestMapping("/editPage")
    public String editPage(HttpServletRequest request, ModelMap mv) {

        long id = CommUtil.null2Long(request.getParameter("id"));
        if(id > 0) {
            SystemRole role = systemRoleBiz.findModel(id);
            mv.put("role", role);
        }
        return "/system/systemRole/edit";
    }

    @RequestMapping("/addOrEdit")
    @ResponseBody
    public JSONObject addOrEdit(HttpServletRequest request) {

        JSONObject retJson = CommUtil.jsonResult(false);
        long id = CommUtil.null2Long(request.getParameter("id"));
        String roleName = CommUtil.null2String(request.getParameter("roleName"));
        String roleDescription = CommUtil.null2String(request.getParameter("roleDescription"));
        SystemRole role = null;
        if(id < 0 || (role = systemRoleBiz.findModel(id)) == null) {
            role = new SystemRole();
            role.setCreateTime(new Date());
        }
        role.setRoleName(roleName);
        role.setRoleDescription(roleDescription);

        //判断角色名是否重复
        if (systemRoleBiz.existRoleName(role)) {
            retJson.put("msg", "您输入的角色名称已存在，请重新输入");
            return retJson;
        }
        systemRoleBiz.addOrUpdate(role);
        retJson = CommUtil.jsonResult(true);

        return retJson;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONObject delete(HttpServletRequest request) {

        JSONObject retJson = CommUtil.jsonResult(false);
        long id = CommUtil.null2Long(request.getParameter("id"));
        SystemRole role = null;
        if(id < 0 || (role = systemRoleBiz.findModel(id)) == null) {
            retJson.put("msg", "未找到数据源");
            return retJson;
        }
        role.setDeleteStatus(true);
        systemRoleBiz.update(role);
        return retJson;
    }

    @RequestMapping("/assignPage")
    public String assignPage(HttpServletRequest request, ModelMap mv) {

        long id = CommUtil.null2Long(request.getParameter("id"));
        List<SystemAuthority> systemAuthorityList = systemAuthorityBiz.findList();
        SystemRole role = systemRoleBiz.findModel(CommUtil.null2Long(id));
        if(role != null) {
            Set<SystemAuthority> authoritySet = role.getAuthoritySet();
            if(systemAuthorityList.size() >0 && authoritySet != null) {
                Iterator<SystemAuthority> it = systemAuthorityList.iterator();
                while (it.hasNext()) {
                    SystemAuthority a1 = it.next();
                    for(SystemAuthority a2 : authoritySet) {
                        if(a2.getId().equals(a1.getId())) {
                            it.remove();
                            break;
                        }
                    }
                }
            }
            mv.put("authoritySet", authoritySet);
        }
        mv.put("systemAuthorityList", systemAuthorityList);

        mv.put("id", id);
        return "/system/systemRole/assign";
    }

    @RequestMapping("/assign")
    @ResponseBody
    public JSONObject assign(HttpServletRequest request) {

        JSONObject retJson = CommUtil.jsonResult(false);
        long id = CommUtil.null2Long(request.getParameter("id"));
        String elements = CommUtil.null2String(request.getParameter("elements"));
        SystemRole role = null;
        if(id < 0 || (role = systemRoleBiz.findModel(id)) == null) {
            retJson.put("msg", "未找到数据源");
            return retJson;
        }
        Set<SystemAuthority> authoritySet = new HashSet<SystemAuthority>();
        if(!elements.equals("")) {
            String[] elementss = elements.split(",");
            for (String element : elementss) {
                SystemAuthority authority = systemAuthorityBiz.findModel(CommUtil.null2Long(element));
                if(authority != null && !authority.isDeleteStatus()) {
                    authoritySet.add(authority);
                }
            }
        }
        role.setAuthoritySet(authoritySet);
        systemRoleBiz.update(role);
        retJson = CommUtil.jsonResult(true);
        return retJson;
    }
}
